#!/usr/bin/python2.5 

import sys
sys.path.append("../../")

import time
from runtime import *
from game import *
from pygame.locals import *


"""
RE-WRITTEN:
"""




def create_pickled_game():
    """
    ############################################################################
    START OF GAME CODE
    """

    # Create a new Game
    mytestGame = Game("bricks - GameBaker Demo")

    # Create a Game-Screen / Level
    mytestgamescreen = mytestGame.gamescreen((800,600),"gamescreen")



    # Load this screen first
    mytestGame.startscreen = "gamescreen"

    # Create a GameObject to show in the game - will later need a sprite attatched
    bottom = mytestGame.gameobject("bottom")
    block = mytestGame.gameobject("block")


    # Say that we want this object to appear immediately in our game-screen
    mytestgamescreen.startobjects.append(bottom)
    mytestgamescreen.startobjects.append(block)



    # Create a score object
    score = mytestGame.gameobject("Score")
    mytestgamescreen.startobjects.append(score)



    # Set up a workstate for our objects


    block.baseworkstate="block"
    wsblock = mytestGame.workstate("block")
    wsblock.actions[EVENT_INIT] = """
self.z = 1
self.x=200
self.y=0
self.vx=0
self.vy=5
self.resize(75,75)
self.vars["matrix"] = [ [0 for i in range(3)] for j in range(3)]

self.vars["matrix"] = random.choice([ \
                        [[0,1,0],[0,1,0],[0,1,0]],
                        [[1,1,0],[0,1,0],[0,1,1]], \
                        [[0,0,0],[1,1,1],[0,1,0]] \
                        ])
"""


    wsblock.actions[EVENT_ITERATION] = """

self.clear()
self.move_velocity()



bottom = gamescreen.get_object("bottom")
relx = (-self.x - self.width + bottom.x + bottom.width) /25
rely = (self.y - bottom.y) /25
# check if we're off the bottom
if self.y + self.height > 599:
    i = 0
    for row in self.vars["matrix"]:
        i+=1
        j = 0
        for col in row:
            j +=1
            if col == 1:
                bottom.vars["matrix"][relx+i-1][j-1]=1

    self.vars["matrix"] = random.choice([ \
                        [[0,1,0],[0,1,0],[0,1,0]],
                        [[1,1,0],[0,1,0],[0,1,1]], \
                        [[0,0,0],[1,1,1],[0,1,0]], \
                        [[0,0,1],[0,0,1],[1,1,1]], \
                        [[0,1,0],[1,1,1],[0,1,0]]
                        ])
    self.y = 0
    self.x = 200

else:
    # draw the block onto this object
    i = 0
    for row in self.vars["matrix"]:
        i+=1
        j = 0
        for col in row:
            j+=1
            if col == 1:

                try:
                    self.draw_sprite("ball",self.height - i*25, self.width - j*25)
                except: print "3K3"

"""

    wsblock.actions[EVENT_KEYDOWN*K_UP] = """
# rotate block
oldmatrix = [row[:] for row in self.vars["matrix"]]

for i in range(3):
    for j in range(3):
        self.vars["matrix"][i][j] = oldmatrix[2-j][2-i]

self.vars["matrix"][0][0] = oldmatrix[2][0]
self.vars["matrix"][0][1] = oldmatrix[1][0]
self.vars["matrix"][0][2] = oldmatrix[0][0]
self.vars["matrix"][1][0] = oldmatrix[2][1]
self.vars["matrix"][1][2] = oldmatrix[0][1]
self.vars["matrix"][2][0] = oldmatrix[2][2]
self.vars["matrix"][2][1] = oldmatrix[1][2]
self.vars["matrix"][2][2] = oldmatrix[0][2]

"""

    wsblock.actions[EVENT_KEYUP*K_LEFT] = """
if self.x > gamescreen.get_object("bottom").x:
    # move left
    self.x -=25

"""

    wsblock.actions[EVENT_KEYUP*K_RIGHT] = """
bottom = gamescreen.get_object("bottom")
if self.x + self.width < bottom.x + bottom.width:
    # move right
    self.x +=25

"""

    bottom.baseworkstate="bottom"
    wsbottom = mytestGame.workstate("bottom")

    wsbottom.actions[EVENT_INIT]="""
self.x=200
self.y=0
self.vx=0
self.vy=0
self.resize(400,600)
self.vars["matrix"] = [ [0 for i in range(16)] for j in range(24)]
self.color=(255,255,255)

"""


    wsbottom.actions[EVENT_ITERATION] = """
self.clear()
# draw the block onto this object
i = 0
for row in self.vars["matrix"]:
    i+=1
    j = 0
    for col in row:
        j+=1
        if col == 1:
            self.draw_sprite("ball",self.width- i*25, self.height - j*25)
"""


    # Tell the GameObject to start with this workstate


    wsscore = mytestGame.workstate("WSScore")

    wsscore.actions[EVENT_INIT] = """
self.x = 0 
self.y = 0
self.color = (255,255,255)
self.resize((60,20))
self.clear()
"""
    wsscore.actions[EVENT_ITERATION] = """

self.clear()
self.draw_text(game.score,20,(255,0,0))
"""
    score.baseworkstate = "WSScore"




    sprite = mytestGame.sprite("ball")

    # The images are in one file, going downwards
    sprite.imagefiles.append({"filename":"sprites/images/ball.bmp", "topleft":(0,0), "bottomright":(24,24)})
    sprite.framerate = 200
    sprite.alphakey = (0,255,0)


    """
    *********************************************************************************
    END OF GAME CODE
    """

    import gbfileio
    gbfileio.savegame("gameouttestyaml.gb",mytestGame,method="pickle")

    game = gbfileio.loadgame("gameouttestyaml.gb")

    run_game(game)




if __name__ == "__main__":
    create_pickled_game()

